package com.firebird.app.fekpsl.adapter.exam

import android.text.Html
import android.text.Html.FROM_HTML_MODE_LEGACY
import android.view.View
import android.widget.RadioButton
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.bean.QuestionTestContentBean
import com.firebird.app.fekpsl.bean.SingleAnswer
import com.google.gson.Gson

/**
 * Created by firebird on 2018/4/22.
 */
class SingleAnswerAdapter : BaseQuickAdapter<SingleAnswer, BaseViewHolder>(R.layout.exam_item_single_answer) {
    inline fun <reified T> Gson.fromJson(json: String) = fromJson(json, T::class.java)
    override fun convert(helper: BaseViewHolder?, item: SingleAnswer?) {
        helper ?: return
        item ?: return
        val index=helper.layoutPosition
        /*
        {"titleContent":"JAVA所定义的版本中不包括：（）","analyze":"a",
            "questionItemObjects":[{"prefix":"A","content":"JAVA2 EE","score":null},
            {"prefix":"B","content":"JAVA2 Card","score":null},
            {"prefix":"C","content":"JAVA2 ME","score":null},
            {"prefix":"D","content":" JAVA2 HE","score":null},
            {"prefix":"E","content":" JAVA2 SE","score":null}],"correct":"D"}
*/
        helper.getView<TextView>(R.id.tv_single_question_a).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_single_question_b).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_single_question_c).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_single_question_d).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_single_question_e).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_single_question_f).visibility = View.GONE
        helper.getView<TextView>(R.id.tv_single_question_g).visibility = View.GONE
        helper.getView<RadioButton>(R.id.rb_single_answer_a).visibility = View.GONE
        helper.getView<RadioButton>(R.id.rb_single_answer_b).visibility = View.GONE
        helper.getView<RadioButton>(R.id.rb_single_answer_c).visibility = View.GONE
        helper.getView<RadioButton>(R.id.rb_single_answer_d).visibility = View.GONE
        helper.getView<RadioButton>(R.id.rb_single_answer_e).visibility = View.GONE
        helper.getView<RadioButton>(R.id.rb_single_answer_f).visibility = View.GONE
        helper.getView<RadioButton>(R.id.rb_single_answer_g).visibility = View.GONE
        val QuestionTestContent=item.content
        val bean = Gson().fromJson<QuestionTestContentBean>(QuestionTestContent)
        val titleContent=bean.titleContent
        helper.setText(R.id.tv_single_question_title, Html.fromHtml(titleContent,FROM_HTML_MODE_LEGACY))
        bean.questionItemObjects.forEachIndexed{ index, s ->
            val question_choice=bean.questionItemObjects.get(index).prefix + "." + bean.questionItemObjects.get(index).content
            when(index) {
                0-> {
                    helper.setText(R.id.tv_single_question_a, question_choice)
                    helper.getView<TextView>(R.id.tv_single_question_a).visibility = View.VISIBLE
                    helper.getView<RadioButton>(R.id.rb_single_answer_a).visibility = View.VISIBLE
                }
                1-> {
                    helper.setText(R.id.tv_single_question_b, question_choice)
                    helper.getView<TextView>(R.id.tv_single_question_b).visibility = View.VISIBLE
                    helper.getView<RadioButton>(R.id.rb_single_answer_b).visibility = View.VISIBLE
                }
                2-> {
                    helper.setText(R.id.tv_single_question_c, question_choice)
                    helper.getView<TextView>(R.id.tv_single_question_c).visibility = View.VISIBLE
                    helper.getView<RadioButton>(R.id.rb_single_answer_c).visibility = View.VISIBLE
                }
                3-> {
                    helper.setText(R.id.tv_single_question_d, question_choice)
                    helper.getView<TextView>(R.id.tv_single_question_d).visibility = View.VISIBLE
                    helper.getView<RadioButton>(R.id.rb_single_answer_d).visibility = View.VISIBLE
                }
                4-> {
                    helper.setText(R.id.tv_single_question_e, question_choice)
                    helper.getView<TextView>(R.id.tv_single_question_e).visibility = View.VISIBLE
                    helper.getView<RadioButton>(R.id.rb_single_answer_e).visibility = View.VISIBLE
                }
                5-> {
                    helper.setText(R.id.tv_single_question_f, question_choice)
                    helper.getView<TextView>(R.id.tv_single_question_f).visibility = View.VISIBLE
                    helper.getView<RadioButton>(R.id.rb_single_answer_f).visibility = View.VISIBLE
                }
                6-> {
                    helper.setText(R.id.tv_single_question_g, question_choice)
                    helper.getView<TextView>(R.id.tv_single_question_g).visibility = View.VISIBLE
                    helper.getView<RadioButton>(R.id.rb_single_answer_g).visibility = View.VISIBLE
                }
            }
        }
        helper.setText(R.id.tv_single_question_no,(index+1).toString())
    }
}
